/*
 * Copyright 2010-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jslet.paas.modelmeta.model;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonWriteNullProperties;

@SuppressWarnings("deprecation")
@Entity
@Table(name = "TMetaField")
@JsonWriteNullProperties(value = false)
public class FieldMeta implements Serializable
{
	private static final long serialVersionUID = 2724993653964595377L;

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "modelId")
	@JsonIgnore
	private ModelMeta modelMeta;

	@JsonIgnore
	private String dbColumn;

	@Id
	@JsonIgnore
	private int fieldId;

	private int index;

	private String name;

	private String label;

	private String groupLabel;

	private String type = FieldMetaType.STRING;

	private int length = 20;

	private int scale;

	private int displayWidth;

	private String displayFormat;

	private String defaultExpr;

	private Serializable defaultValue;

	private String alignment;

	private String editControl;

	private String formula;

	private boolean readOnly;

	private boolean visible = true;

	private boolean exchged;

	private boolean required;

	private String range;

	private String regularExpr;

	private String lookupField;

	private String urlExpr;

	private String urlTarget;

	private boolean betweenStyle;

	private String maxValueField;

	private boolean clientTranslate = true;

	private String inputValueField;

	private String displayValueField;

	private String subDataset;

	private boolean oneToOne;

	private boolean clientField;

	@JsonIgnore
	private String subDatasetFk;

	public ModelMeta getModelMeta()
	{
		return modelMeta;
	}

	public void setModelMeta(ModelMeta model)
	{
		this.modelMeta = model;
	}

	public int getFieldId()
	{
		return fieldId;
	}

	public void setFieldId(int fieldid)
	{
		this.fieldId = fieldid;
	}

	public int getIndex()
	{
		return index;
	}

	public void setIndex(int index)
	{
		this.index = index;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name == null ? name : name.trim();
	}

	public String getLabel()
	{
		return label;
	}

	public void setLabel(String label)
	{
		this.label = label;
	}

	public String getGroupLabel()
	{
		return groupLabel;
	}

	public void setGroupLabel(String groupLabel)
	{
		this.groupLabel = groupLabel;
	}

	public String getType()
	{
		return type;
	}

	public void setType(String type)
	{
		if (type != null)
		{
			this.type = type;
		}
	}

	public String getDbColumn()
	{
		return dbColumn;
	}

	public void setDbColumn(String dbColumn)
	{
		if ("A".equalsIgnoreCase(this.type))
		{
			dbColumn = null;
		}

		if (dbColumn != null)
		{
			dbColumn = dbColumn.trim();
			if (dbColumn.length() > 0)
			{
				this.dbColumn = dbColumn;
				this.clientField = false;
				return;
			}
		}
		this.dbColumn = null;
	}

	public boolean isRequired()
	{
		return required;
	}

	public void setRequired(boolean required)
	{
		this.required = required;
	}

	public int getLength()
	{
		return length;
	}

	public void setLength(int length)
	{
		this.length = length;
	}

	public int getScale()
	{
		return scale;
	}

	public void setScale(int scale)
	{
		this.scale = scale;
	}

	public int getDisplayWidth()
	{
		return displayWidth;
	}

	public void setDisplayWidth(int displayWidth)
	{
		this.displayWidth = displayWidth;
	}

	public String getDisplayFormat()
	{
		return displayFormat;
	}

	public void setDisplayFormat(String displayFormat)
	{
		this.displayFormat = displayFormat;
	}

	public String getDefaultExpr()
	{
		return defaultExpr;
	}

	public void setDefaultExpr(String defaultExpr)
	{
		this.defaultExpr = defaultExpr;
	}

	public Serializable getDefaultValue()
	{
		return defaultValue;
	}

	public void setDefaultValue(Serializable defaultValue)
	{
		this.defaultValue = defaultValue;
	}

	public String getAlignment()
	{
		return alignment;
	}

	public void setAlignment(String alignment)
	{
		this.alignment = alignment;
	}

	public String getEditControl()
	{
		return editControl;
	}

	public void setEditControl(String editControl)
	{
		this.editControl = editControl;
	}

	public String getFormula()
	{
		return formula;
	}

	public void setFormula(String formula)
	{
		this.formula = formula;
	}

	public boolean isReadOnly()
	{
		return readOnly;
	}

	public void setReadOnly(boolean readOnly)
	{
		this.readOnly = readOnly;
	}

	public boolean isVisible()
	{
		return visible;
	}

	public void setVisible(boolean visible)
	{
		this.visible = visible;
	}

	public boolean isExchged()
	{
		return exchged;
	}

	public void setExchged(boolean exchged)
	{
		this.exchged = exchged;
	}

	public String getRange()
	{
		return range;
	}

	public void setRange(String range)
	{
		this.range = range;
	}

	public String getRegularExpr()
	{
		return regularExpr;
	}

	public void setRegularExpr(String regularExpr)
	{
		this.regularExpr = regularExpr;
	}

	public String getLookupField()
	{
		return lookupField;
	}

	public void setLookupField(String lookupField)
	{
		this.lookupField = lookupField;
	}

	public String getUrlExpr()
	{
		return urlExpr;
	}

	public void setUrlExpr(String urlExpr)
	{
		this.urlExpr = urlExpr;
	}

	public String getUrlTarget()
	{
		return urlTarget;
	}

	public void setUrlTarget(String urlTarget)
	{
		this.urlTarget = urlTarget;
	}

	public boolean isBetweenStyle()
	{
		return betweenStyle;
	}

	public void setBetweenStyle(boolean betweenStyle)
	{
		this.betweenStyle = betweenStyle;
	}

	public String getMaxValueField()
	{
		return maxValueField;
	}

	public void setMaxValueField(String maxValueField)
	{
		this.maxValueField = maxValueField;
	}

	public boolean isClientTranslate()
	{
		return clientTranslate;
	}

	public void setClientTranslate(boolean clientTranslate)
	{
		this.clientTranslate = clientTranslate;
	}

	public String getInputValueField()
	{
		return inputValueField;
	}

	public void setInputValueField(String inputValueField)
	{
		this.inputValueField = inputValueField;
	}

	public String getDisplayValueField()
	{
		return displayValueField;
	}

	public void setDisplayValueField(String displayValueField)
	{
		this.displayValueField = displayValueField;
	}

	public String getSubDataset()
	{
		return subDataset;
	}

	public void setSubDataset(String subDataset)
	{
		this.subDataset = subDataset;
	}

	public boolean isOneToOne()
	{
		return oneToOne;
	}

	public void setOneToOne(boolean oneToOne)
	{
		this.oneToOne = oneToOne;
	}

	public String getSubDatasetFk()
	{
		return subDatasetFk;
	}

	public void setSubDatasetFk(String subDatasetFk)
	{
		this.subDatasetFk = subDatasetFk;
	}

	public boolean isClientField()
	{
		return clientField;
	}

	public void setClientField(boolean clientField)
	{
		this.clientField = this.dbColumn == null ? clientField : false;
	}

	@Override
	public boolean equals(Object obj)
	{
		if (obj instanceof FieldMeta)
		{
			return this.getFieldId() == ((FieldMeta) obj).getFieldId();
		}
		return false;
	}

	@Override
	public String toString()
	{
		return super.toString() + "[" + this.getName() + "]";
	}

	public void copyFrom(FieldMeta bizFld)
	{
		if (bizFld == null)
		{
			return;
		}
		this.setDbColumn(bizFld.getDbColumn());
		this.setIndex(bizFld.getIndex());
		this.setName(bizFld.getName());
		this.setType(bizFld.getType());
		this.setLength(bizFld.getLength());
		this.setScale(bizFld.getScale());
		this.setDisplayWidth(bizFld.getDisplayWidth());
		this.setDisplayFormat(bizFld.getDisplayFormat());
		this.setDefaultExpr(bizFld.getDefaultExpr());
		this.setAlignment(bizFld.getAlignment());
		this.setEditControl(bizFld.getEditControl());
		this.setFormula(bizFld.getFormula());
		this.setReadOnly(bizFld.isReadOnly());
		this.setVisible(bizFld.isVisible());
		this.setExchged(bizFld.isExchged());
		this.setRequired(bizFld.isRequired());
		this.setRange(bizFld.getRange());
		this.setRegularExpr(bizFld.getRegularExpr());
		this.setLookupField(bizFld.getLookupField());
		this.setUrlExpr(bizFld.getUrlExpr());
		this.setUrlTarget(bizFld.getUrlTarget());
		this.setBetweenStyle(bizFld.isBetweenStyle());
		this.setMaxValueField(bizFld.getMaxValueField());
		this.setClientTranslate(bizFld.isClientTranslate());

		this.setInputValueField(bizFld.getInputValueField());
		this.setDisplayValueField(bizFld.getDisplayValueField());
		this.setSubDataset(bizFld.getSubDataset());
		this.setSubDatasetFk(bizFld.getSubDatasetFk());
		this.setOneToOne(bizFld.isOneToOne());
		this.setClientField(bizFld.isClientField());
	}
}
